﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;

namespace WJSHWebApi.Common
{
    public static class Extend
    {
        public static  string GetTableName<T>()
        {
            string tablename = null;
            var type = typeof(T);
            if (type.IsDefined(typeof(TableAttribute), true))
            {
                var aAttributeArry = type.GetCustomAttributes(typeof(TableAttribute), true).FirstOrDefault();
                if (aAttributeArry != null)
                {
                    tablename = ((TableAttribute)aAttributeArry).Name;
                }
            }
            return tablename;
        }


        public static string UpdateSql<T>(T t) where T : class
        {
            Type type = t.GetType();
            string tableName = GetTableName<T>();
            StringBuilder sql = new StringBuilder();
            sql.Append($"update {tableName} set ");

            var ps = type.GetProperties();
            string IdValue = string.Empty;
            foreach (var p in ps)
            {
                //带有索引器的字段名
                string pName = p.Name;
                //获取属性
                var pType = p.PropertyType;
                //获取值
                object obj = p.GetValue(t);
                string value = null;
                if (pType == typeof(string))
                {
                    value = $"'{obj}'";
                }
                else if (pType == typeof(bool))
                {
                    value = (bool)obj ? "1" : "0";
                }
                else
                {
                    value = $"{obj}";
                }
                if (pName.ToLower() == "Id")
                {
                    IdValue = $"where {pName}={value}";
                }
                else
                {
                    sql.Append($"{pName}={value},");
                }
            }
            sql.Remove(sql.Length - 1, 1);
            sql.Append(IdValue);
            return sql.ToString();
        }

        public static string AddSql<T>() where T : class
        {
            string tableName = GetTableName<T>();
            StringBuilder sql = new StringBuilder();
            sql.Append($"insert into {tableName}(");
            Type type = typeof(T);
            var ps = type.GetProperties();
            string IdValue = string.Empty;
            foreach (var p in ps)
            {
                //带有索引器的字段名
                string pName = p.Name;
                if (pName.ToLower() == "Id") continue;
                sql.Append($"{pName},");
            }
            sql.Remove(sql.Length - 1, 1);
            sql.Append($") values(");
            foreach (var p in ps)
            {
                //带有索引器的字段名
                string pName = p.Name;
                if (pName.ToLower() == "Id") continue;

                sql.Append($"@{pName},");
            }
            sql.Remove(sql.Length - 1, 1);
            sql.Append($")");
            sql.Append(IdValue);
            return sql.ToString();
        }


    }
}
